/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.scroll;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

/**
 * 
 * @author Rafael Fernandes
 */
public abstract class ScrollableLayer extends FrameLayout {
    private static final int SCROLL_DELAY = 600,
                             SCROLL_OUTSIDE_ZONE = 0,
                             SCROLL_ZONE = 20,
                             SCROLL_WAITING_IN_ZONE = 1,
                             SCROLL_LEFT = 0,
                             SCROLL_RIGHT = 1;
    
    private int scrollState = SCROLL_OUTSIDE_ZONE;
    private ScrollRunnable scrollThread = new ScrollRunnable();

    public ScrollableLayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        final float x = ev.getX();

        switch (action) {
        case MotionEvent.ACTION_DOWN:
            if ((x < SCROLL_ZONE) || (x > getWidth() - SCROLL_ZONE)) {
                scrollState = SCROLL_WAITING_IN_ZONE;
                postDelayed(scrollThread, SCROLL_DELAY);
            } else {
                scrollState = SCROLL_OUTSIDE_ZONE;
            }

            break;
        case MotionEvent.ACTION_MOVE:
            if (x < SCROLL_ZONE) {
                if (scrollState == SCROLL_OUTSIDE_ZONE) {
                    scrollState = SCROLL_WAITING_IN_ZONE;
                    scrollThread.setDirection(SCROLL_LEFT);
                    postDelayed(scrollThread, SCROLL_DELAY);
                }
            } else if (x > getWidth() - SCROLL_ZONE) {
                if (scrollState == SCROLL_OUTSIDE_ZONE) {
                    scrollState = SCROLL_WAITING_IN_ZONE;
                    scrollThread.setDirection(SCROLL_RIGHT);
                    postDelayed(scrollThread, SCROLL_DELAY);
                }
            } else {
                if (scrollState == SCROLL_WAITING_IN_ZONE) {
                    scrollState = SCROLL_OUTSIDE_ZONE;
                    scrollThread.setDirection(SCROLL_RIGHT);
                    removeCallbacks(scrollThread);
                }
            }

            break;
        case MotionEvent.ACTION_UP:
            removeCallbacks(scrollThread);
            break;
        }

        return true;
    }
    
    abstract void scrollLeft();
    abstract void scrollRight();

    private class ScrollRunnable implements Runnable {
        private int direction;

        ScrollRunnable() {
        }

        public void run() {
            if (direction == SCROLL_LEFT) {
                scrollLeft();
            } else {
                scrollRight();
            }
            scrollState = SCROLL_OUTSIDE_ZONE;
        }

        void setDirection(int direction) {
            this.direction = direction;
        }
    }    
}
